package com.easyadmin.entity.enums;


import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public enum ResponseCodeEnum {

    CODE_200(200, "请求成功"),
    CODE_400(400, "业务错误"),
    CODE_404(404, "请求地址不存在"),
    CODE_405(405, "接口未设置拦截器"),
    CODE_600(600, "请求参数不正确"),
    CODE_601(601, "数据转换失败"),
    CODE_602(602, "请求地址不存在"),
    CODE_500(500, "服务异常"),
    CODE_901(901, "登录超时"),
    CODE_902(902, "没有操作权限"),
    CODE_903(903, "Http请求超时"),
    CODE_904(904, "未获取到信息");


    private Integer code;
    private String desc;

    ResponseCodeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static ResponseCodeEnum getByCode(Integer code) {
        for (ResponseCodeEnum item : ResponseCodeEnum.values()) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }


    @Override
    public String toString() {
        Map<Integer, String> result = new HashMap<>();
        result.put(code, desc);
        return JSONObject.toJSONString(result);
    }
}
